/*******************************************************************************
 * This file is part of blueprint4j www.blueprint4j.org.
 * 
 *     blueprint4j is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     blueprint4j is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Lesser General Public License for more details.
 * 
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with blueprint4j. If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package blueprint4j.db;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Connection;

import blueprint4j.utils.*;

import java.text.SimpleDateFormat;

public class FieldTimestamp extends FieldDateTime {
    
    private Date date = null;
    private VectorField add_field= new VectorField(),exclude_field=new VectorField();
    
    /**
     * public FieldTimestamp( String p_name, String p_description, boolean p_key_field)
     *
     * Name: Name of field -> equivalent to table.field_name
     * Description: Description of field
     * key_field: Is this field a key field in the table (index field)
     */
    public FieldTimestamp( String p_name, int p_field_type ,String p_description,Entity entity ) {
        super( p_name,p_field_type,p_description,entity );
    }
    
    public FieldTimestamp( String p_name, int p_field_type ,String p_description,Field exclude[],Field extra[],Entity entity ) {
        this(p_name,p_field_type,p_description,entity );
        includeAllField();
        addExcludeField(exclude);
        if (extra !=null) {
            addIncludeField(extra);
        }
    }
    
    public Field newInstance(Entity entity) {
        return new FieldTimestamp(getName(),getFieldType(),getDescription(),entity);
    }
    
    public void includeAllField() {
        for (int i =0;i <getEntity().getFields().size();i++) {
            if (!(getEntity().getFields().get(i) instanceof FieldTimestamp)) {
                add_field.add(getEntity().getFields().get(i));
            }
        }
    }
    
    public void addIncludeField(Field field[]) {
        add_field.add(field);
    }
    
    public void addExcludeField(Field field[]) {
        for (int i = 0;i < add_field.size();i++) {
            for (int e = 0;e < field.length;e++) {
                if (add_field.get(i).getName().equals(field[e].getName())){
                    add_field.remove(i);
                }
            }
        }
    }
    
    public void setTimestamp(Date value) {
        this.date = value;
        super.set(value);
    }
    
    /**
     * Return data in a suitable SQL format
     */
    public String getSQLValue()
    throws DataException {
        if (date == null) {
            // CHECK IF ANY KEY FIELDS HAS CHANGED
            if (add_field.size()==0 || (!getEntity().isLoaded() || add_field.isChanged())) {
                return entity.getConnection().getDataBaseUtils().currentDateTimeSQLMethod();
            } else {
                return null;
            }
        } else {
            return entity.getConnection().getDataBaseUtils().convertDateTimeToSQLString(date);
        }
    }
}
